package view;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JPanel;

import pojo.*;

import callback.ChooseRoomCallback;
import callback.CreateLeagueRoomCallback;
import callback.CreateRoomCallback;
import callback.JoinLeagueCallback;
import callback.LeagueCallback;
import callback.LobbyBackBtnCallback;
import callback.UpdateRoomCallback;

import client.Client;

import util.Utils;
import widget.LeagueWidget;
import widget.MyButtonWidget;
import widget.RoomWidget;
import widget.UserInfoWidget;

public class LobbyView extends ChessOnlineView implements MouseListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -366828517363901737L;
	private UserInfoWidget mUserInfoWidget;
	private List<MyButtonWidget> mRooms = new ArrayList<>();
	private List<MyButtonWidget> mLeagues = new ArrayList<>();
	private int mCurrentPageIndex = 0;
	public static List<ChessOnlineRoom> rooms = new ArrayList<ChessOnlineRoom>();
	public static List<League> leagues = new ArrayList<League>();
	public static List<ChessOnlineRoom> leaguerooms = new ArrayList<ChessOnlineRoom>();

	public static List<League> getLeagues() {
		return leagues;
	}

	public static void setLeagues(List<League> leagues) {
		LobbyView.leagues = leagues;
	}

	public List<ChessOnlineRoom> getRooms() {
		return rooms;
	}

	public static void setRooms(List<ChessOnlineRoom> rooms) {
		LobbyView.rooms = rooms;
	}

	// private JPanel mLobbyContainer;
	private MyButtonWidget mNextPageBtn;
	private MyButtonWidget mPrevPageBtn;
	private MyButtonWidget mBackBtn;
	private MyButtonWidget mCreateRoomBtn;
	private MyButtonWidget mLeagueBtn;

	public LobbyView(Client mainScreen, boolean isLeague) {
		super(mainScreen);

		mUserInfoWidget = new UserInfoWidget(mMainScreen.getAcc(), 10, 30);

		JPanel userPanel = new JPanel();
		userPanel.setSize(200, 600);
		userPanel.add(mUserInfoWidget);

		// mLobbyContainer = new JPanel();
		// mLobbyContainer.setSize(600, 600);

		mNextPageBtn = new MyButtonWidget(440, 475,
				"Resources/Images/Shift_Icon_Right.png",
				"Resources/Images/Shift_Icon_Right.png", "",
				new UpdateRoomCallback(this, 1));
		mPrevPageBtn = new MyButtonWidget(340, 475,
				"Resources/Images/Shift_Icon_Left.png",
				"Resources/Images/Shift_Icon_Left.png", "",
				new UpdateRoomCallback(this, -1));

		if (isLeague) {
			mBackBtn = new MyButtonWidget(650, 450,
					"Resources/Images/back-icon.png",
					"Resources/Images/back-icon.png", "",
					new LobbyBackBtnCallback(mMainScreen, this));
			mCreateRoomBtn = new MyButtonWidget(20, 245,
					"Resources/Images/create-btn-league.png",
					"Resources/Images/create-btn-league.png", "",
					new CreateLeagueRoomCallback(mMainScreen));
		} else {
			mBackBtn = new MyButtonWidget(650, 450,
					"Resources/Images/back-icon.png",
					"Resources/Images/back-icon.png", "",
					new LobbyBackBtnCallback(mMainScreen, this));
			mCreateRoomBtn = new MyButtonWidget(20, 245,
					"Resources/Images/create-btn.png",
					"Resources/Images/create-btn.png", "",
					new CreateRoomCallback(mMainScreen));
		}

		mLeagueBtn = new MyButtonWidget(10, 400,
				"Resources/Images/league-icon.png",
				"Resources/Images/league-icon.png", "", new LeagueCallback(
						mMainScreen));

		this.add(userPanel, BorderLayout.WEST);
		// this.add(mLobbyContainer, BorderLayout.EAST);
		this.add(mNextPageBtn);
		this.add(mPrevPageBtn);
		this.add(mBackBtn);
		this.add(mCreateRoomBtn);
		this.add(mLeagueBtn);
		this.addMouseListener(this);

	}

	/*
	 * public void addRoom(ChessOnlineRoom r) { synchronized (this) { // TODO:
	 * Hard code the room info
	 * 
	 * 
	 * UserAccount playerA = new UserAccount("theturtle232", "123", "Riki", 100,
	 * 10, 1, null); UserAccount playerB = new UserAccount("B1", "123",
	 * "Boundy", 200, 3, 5, null); rooms.add(new ChessOnlineRoom(1, "Name",
	 * playerA, playerB, 10, false)); rooms.add(new ChessOnlineRoom(2, "Name",
	 * playerA, null, 20, false)); rooms.add(new ChessOnlineRoom(3, "Name",
	 * playerA, playerB, 30, false)); rooms.add(new ChessOnlineRoom(1, "Name",
	 * playerA, null, 10, false)); rooms.add(new ChessOnlineRoom(2, "Name",
	 * playerA, null, 20, false)); rooms.add(new ChessOnlineRoom(3, "Name",
	 * playerA, playerB, 30, false)); rooms.add(new ChessOnlineRoom(1, "Name",
	 * playerA, null, 10, false)); rooms.add(new ChessOnlineRoom(2, "Name",
	 * playerA, playerB, 20, false)); rooms.add(new ChessOnlineRoom(2, "Name",
	 * playerA, null, 20, false)); rooms.add(new ChessOnlineRoom(2, "Name",
	 * playerA, null, 20, false));
	 * 
	 * 
	 * // mLobbyContainer.removeAll(); // rooms.add(r);
	 * 
	 * mRooms.clear(); mLeagues.clear();
	 * 
	 * int initX; int initY = 20; int deltaX = 150; int deltaY = 150; int
	 * maxRoomPerRow = 3; int maxRoomPerCol = 4; String roomBg = null; String
	 * fullRoomBg = null;
	 * 
	 * roomBg = "Resources/Images/white-piece.png"; fullRoomBg =
	 * "Resources/Images/black-piece.png";
	 * 
	 * for(int i = 0; i < maxRoomPerRow && i * maxRoomPerRow < rooms.size();
	 * i++) { initX = 190; for(int j = 0; j < maxRoomPerCol; j++) {
	 * ChessOnlineRoom room = rooms.get(i * maxRoomPerRow + j); String bg =
	 * room.getPlayerB() == null ? roomBg : fullRoomBg; MyButtonWidget cmd = new
	 * MyButtonWidget(initX , initY, bg, bg, String.valueOf(room.getRoomId()),
	 * new ChooseRoomCallback(mMainScreen, room)); if(room.getPlayerB() != null)
	 * { cmd.disable(); }
	 * 
	 * this.mRooms.add(cmd);
	 * 
	 * if(i * maxRoomPerRow + j == rooms.size() - 1) break;
	 * 
	 * initX += deltaX; }
	 * 
	 * initY += deltaY; }
	 * 
	 * repaintComponent();
	 * 
	 * } }
	 */

	private void repaintComponent() {
		this.removeAll();

		this.add(mUserInfoWidget);
		this.add(mNextPageBtn);
		this.add(mPrevPageBtn);
		this.add(mBackBtn);
		this.add(mCreateRoomBtn);
		this.add(mLeagueBtn);

		for (MyButtonWidget room : mRooms) {
			this.add(room);
		}

		for (MyButtonWidget league : mLeagues) {
			this.add(league);
		}

		revalidate();
		repaint();
	}

	public void showRoom() {
		int maxRoomPerRow = 4; // col
		int maxRoomPerCol = 3;// row
		if (mCurrentPageIndex < 0)
			mCurrentPageIndex = 0;
		System.out.println("Page index : " + mCurrentPageIndex);

		// rooms.remove(null);
		// addRoom();

		System.out.println("rooms : " + rooms.size());

		mRooms.clear();
		mLeagues.clear();

		int initX = 190;
		int initY = 20;
		int deltaX = 150;
		int deltaY = 150;

		int breakRow = 0;
		String roomBg = null;
		String fullRoomBg = null;

		roomBg = "Resources/Images/RoomIcon.png";
		fullRoomBg = "Resources/Images/RoomIconFull.png";
		int maxPage = rooms.size() % (maxRoomPerRow * maxRoomPerCol);
		if (maxPage == 0) {
			maxPage = rooms.size() / (maxRoomPerRow * maxRoomPerCol) - 1;
		} else {
			maxPage = rooms.size() / (maxRoomPerRow * maxRoomPerCol);
		}
		if (mCurrentPageIndex > maxPage) {
			mCurrentPageIndex--;
			return;
		}

		int fromIndex = mCurrentPageIndex * maxRoomPerRow * maxRoomPerCol;

		System.out.println("from index " + fromIndex);
		int toIndex = fromIndex + maxRoomPerRow * maxRoomPerCol;
		if (toIndex > rooms.size()) {
			toIndex = rooms.size();
		}
		System.out.println("to index " + toIndex);
		List<ChessOnlineRoom> listRoomInCurrentPage = rooms.subList(fromIndex,
				toIndex);

		for (int i = 0; i < maxRoomPerCol
				&& i * maxRoomPerRow < listRoomInCurrentPage.size(); i++) {
			// breakRow = 0;
			for (int j = 0; j < maxRoomPerRow; j++) {

				ChessOnlineRoom room = listRoomInCurrentPage.get(i* maxRoomPerRow + j);
				if (room != null) {
					if (!room.isLeague()) {
						String bg = room.getPlayerB() == null ? roomBg
								: fullRoomBg;
						MyButtonWidget cmd = new RoomWidget(initX, initY, bg,bg, room, new ChooseRoomCallback(mMainScreen,room));
						if (room.getPlayerB() != null) {
							cmd.disable();
						}

						this.mRooms.add(cmd);
						initX += deltaX;
						breakRow++;

						// System.out.println("kq la: "+ i + " - " + j);

					}			
				}
				if (breakRow == maxRoomPerRow) {
					initY += deltaY;
					initX = 190;
					breakRow = 0;
				}
				if (i * maxRoomPerRow + j == rooms.size() - 1)
					break;
			}

		}

		repaintComponent();
	}

	public void showLeague() {
		synchronized (this) {
			// Enter the League Mode
			Utils.enterLeagueMode();

			mRooms.clear();
			mLeagues.clear();

			// TODO: Get all league from server and then init the league icon
			// with the info

			int initX = 190;
			int initY = 10;
			int deltaX = 150;
			int deltaY = 150;
			int maxLeaguePerRow = 4;
			int maxLeaguePerCol = 3;
			int breakRow = 0;
			String leagueBg = "Resources/Images/league-icon.png";
			String leagueBlackBg = "Resources/Images/league-icon-black.png";
			Date date = new Date();

			for (int i = 0; i < maxLeaguePerCol
					&& i * maxLeaguePerRow < leagues.size(); i++) {
				for (int j = 0; j < maxLeaguePerRow; j++) {
					League league = leagues.get(i * maxLeaguePerRow + j);
					if (league.getStartDate() != null
							&& league.getEndDate() != null
							&& league.getReward() != null) {
						String bg = leagueBg;
						int duration = 0;
						if (date.compareTo(league.getStartDate()) < 0) {
							bg = leagueBlackBg;
							duration = 1;
						} else if (date.compareTo(league.getEndDate()) > 0) {
							bg = leagueBlackBg;
							duration = -1;
						}

						MyButtonWidget cmd = new LeagueWidget(initX, initY, bg,
								bg, league, duration, new JoinLeagueCallback(
										mMainScreen, league));
						if (bg.equals(leagueBlackBg))
							cmd.disable();
						this.mLeagues.add(cmd);
						initX += deltaX;
						breakRow++;

						if (i * maxLeaguePerRow + j == leagues.size() - 1)
							break;
					}
					if (breakRow == maxLeaguePerRow) {
						initY += deltaY;
						initX = 190;
						breakRow = 0;
					}

					if (i * maxLeaguePerRow + j == leagues.size() - 1)
						break;
				}
			}

			repaintComponent();
		}
	}

	public void showLeagueRooms(int leagueId) {
		synchronized (this) {
			// Save the League Id
			Utils.saveLeagueId(leagueId);

			mRooms.clear();
			mLeagues.clear();

			int initX = 190;
			int initY = 20;
			int deltaX = 150;
			int deltaY = 150;
			int maxRoomPerRow = 4;
			int maxRoomPerCol = 3;
			int breakRow = 0;
			String roomBg = null;
			String fullRoomBg = null;

			roomBg = "Resources/Images/RoomIcon.png";
			fullRoomBg = "Resources/Images/RoomIconFull.png";

			for (int i = 0; i < maxRoomPerCol
					&& i * maxRoomPerRow < leaguerooms.size(); i++) {
				for (int j = 0; j < maxRoomPerRow; j++) {
					ChessOnlineRoom leagueroom = leaguerooms.get(i
							* maxRoomPerRow + j);
					String bg = leagueroom.getPlayerB() == null ? roomBg
							: fullRoomBg;
					MyButtonWidget cmd = new RoomWidget(initX, initY, bg, bg,
							leagueroom, new ChooseRoomCallback(mMainScreen,
									leagueroom));
					if (leagueroom.getPlayerB() != null) {
						cmd.disable();
					}

					this.mRooms.add(cmd);

					if (i * maxRoomPerRow + j == leaguerooms.size() - 1)
						break;

					initX += deltaX;
					breakRow++;

					if (breakRow == maxRoomPerRow) {
						initY += deltaY;
						initX = 190;
						breakRow = 0;
					}
				}
			}

			repaintComponent();
		}
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		mUserInfoWidget.paint(g);
		mNextPageBtn.paint(g);
		mPrevPageBtn.paint(g);
		mBackBtn.paint(g);
		mCreateRoomBtn.paint(g);
		mLeagueBtn.paint(g);

		for (MyButtonWidget room : mRooms) {
			room.paint(g);
		}

		for (MyButtonWidget league : mLeagues) {
			league.paint(g);
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		synchronized (this) {

			for (MyButtonWidget room : mRooms) {
				room.onClick(e);
			}

			// TODO: When draw the League room, this will clear the current
			// mLeagues list,
			// so we need the synchronized to control the concurrent issue. But
			// for now,
			// I don't know why I cannot use the synchronized function for do it
			// Please help me find the solution!
			for (MyButtonWidget league : mLeagues) {
				league.onClick(e);
			}
		}

		mNextPageBtn.onClick(e);
		mPrevPageBtn.onClick(e);
		mBackBtn.onClick(e);
		mCreateRoomBtn.onClick(e);
		mLeagueBtn.onClick(e);
	}

	@Override
	public void mouseEntered(MouseEvent e) {

	}

	@Override
	public void mouseExited(MouseEvent e) {

	}

	@Override
	public void mousePressed(MouseEvent e) {

	}

	@Override
	public void mouseReleased(MouseEvent e) {

	}

	public int getCurrentPageIndex() {
		return mCurrentPageIndex;
	}

	public void setCurrentPageIndex(int currentPageIndex) {
		this.mCurrentPageIndex = currentPageIndex;
	}

}
